
package com.example.mam.web;

import com.example.mam.dto.BaseResult;
import com.example.mam.dto.TableResult;
import com.example.mam.entity.Manager;
import com.example.mam.exception.BizException;
import com.example.mam.exception.BizExceptionEnum;
import com.example.mam.exception.BizExceptionMessage;
import com.example.mam.service.ManagerService;
import com.example.mam.util.Condition;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private ManagerService managerService;


    /**
     * @param email    邮箱
     * @param password 密码
     * @return BaseResult
     */
    @PostMapping("/loginOn")
    public BaseResult<Object> loginOn(String email, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(email, password);
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            if (UnknownAccountException.class.getName().equals(e.getClass().getName())) {
                return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.INVALID_USER));
            }
            if (IncorrectCredentialsException.class.getName().equals(e.getClass().getName())) {
                return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.INVALID_PASSWORD));
            }
            return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.LOGIN_ERROR));
        }
        Manager manager = managerService.getManagerByEmail(email);
        System.out.print(email);
        System.out.print(password);
        return new BaseResult<>(true, manager);
    }

    //登出
    @RequiresUser
    @GetMapping("loginOut")
    public BaseResult<Object> loginOut() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        return new BaseResult<Object>(true, null);
    }


    /**
     * 删除管理员
     *
     * @param managerId 管理员ID
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public BaseResult<Object> deleteManager(@PathVariable("id") Long managerId) {
        try {
            managerService.deleteManagerById(managerId);
        } catch (BizException e) {
            return new BaseResult<Object>(true, new BizExceptionMessage(e.getBizExceptionEnum()));
        }
        return new BaseResult<Object>(true, null);
    }

    /**
     * 冻结管理员账户
     *
     * @param managerId 管理员ID
     * @return
     */
    @GetMapping("/freeze/{id}")
    public BaseResult<Object> freezeManager(@PathVariable("id") Long managerId) {
        if (managerService.freezeManagerAccount(managerId) > 0)
            return new BaseResult<Object>(true, null);
        return new BaseResult<Object>(false, null);
    }

    /**
     * 解冻管理员账户
     *
     * @param managerId 管理员ID
     * @return
     */
    @GetMapping("/thew/{id}")
    public BaseResult<Object> thewManager(@PathVariable("id") Long managerId) {
        if (managerService.thawManagerAccount(managerId) > 0)
            return new BaseResult<Object>(true, null);
        return new BaseResult<Object>(false, null);
    }

    /**
     * 重新设置密码
     *
     * @param manager manager
     * @return BaseResult
     */
    @PutMapping("/resetPassword")
    public BaseResult<Object> restPassword(Manager manager) {
        System.out.println(manager);
        if (managerService.restPassword(manager) > 0)
            return new BaseResult<Object>(true, null);
        return new BaseResult<Object>(false, null);
    }

    /**
     * 添加管理员用户
     *
     * @param manager 管理员对象
     * @return
     */
    @PostMapping("/addManager")
    public BaseResult<Object> addManager(Manager manager) {
        try {
            managerService.addManager(manager);
        } catch (BizException e) {
            if (BizExceptionEnum.EMAIL_REPEAT_ERROR.getCode() == e.getBizExceptionEnum().getCode()) {
                return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.EMAIL_REPEAT_ERROR));
            }

        }

        return new BaseResult<Object>(true, manager);
    }


    /**
     * 管理员列表
     *
     * @param condition 分页条件
     * @return
     */
    @GetMapping("/listManager")
    public BaseResult<Object> listManager(Condition condition) {
        List<Object> list;
        try {
            list = managerService.selectManagerByCondition(condition);
            System.out.println(condition != null ? condition.toString() : "123");
        } catch (AuthenticationException e) {
            return new BaseResult<Object>(false, new BizExceptionMessage(BizExceptionEnum.INNER_ERROR));
        }
        return new BaseResult<Object>(true, new TableResult(condition.getTotal(), list));
    }


}
